<?php
include("/usr/share/jpgraph/jpgraph.php");
include("/usr/share/jpgraph/jpgraph_line.php");
include("/usr/share/jpgraph/jpgraph_bar.php");

require_once("config.php");
require_once("CMySQL.php");

define("NB_ANNEE", 3);

$graph=new Graph(800, 600, "auto");
//$graph->title->Set("Chiffre d'affaire - Nombre de facture (mois/mois)");
$graph->title->Set("Chiffre d'affaire mois/mois");
$graph->title->SetFont(FF_VERDANA,FS_NORMAL,12);
$graph->SetScale("textlin");
$graph->xaxis->SetTickLabels(array("Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"));
$graph->xaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->xaxis->SetLabelAngle(50);
$graph->yaxis->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->legend->SetFont(FF_VERDANA,FS_NORMAL,10);
$graph->legend->pos(0.005, 0.005);
//$graph->SetY2Scale("lin");

$dbName="";
if(isset($_GET["dossier"])) {
	$dbName=CMySQL::getInstance()->getDbNameFromDossier($_GET["dossier"]);
}

$sql="SELECT DATE_FORMAT(creation, \"%Y-%m\") AS periode, SUM(prixUnitaireHT*quantite*IF(document=\"A\",-1,1)) AS ca ";
$sql.="FROM teteFactures INNER JOIN ligneFactures ON facture=code ";
$sql.="WHERE YEAR(creation) >= YEAR(DATE_SUB(CURDATE(), INTERVAL ".(NB_ANNEE-1)." YEAR)) ";
$sql.="GROUP BY periode ORDER BY periode";

$sqlAFact="SELECT DATE_FORMAT(creation, \"%Y-%m\") AS periode, SUM(prixUnitaireHT*quantite*IF(document=\"A\",-1,1)) AS ca ";
$sqlAFact.="FROM ligneBLs INNER JOIN teteBLs ON bl = code ";
$sqlAFact.="WHERE blFacture(code)=0 AND YEAR(creation) >= YEAR(DATE_SUB(CURDATE(), INTERVAL ".(NB_ANNEE-1)." YEAR)) ";
$sqlAFact.="GROUP BY periode";

CMySQL::getInstance()->selectDb($dbName);

$result=CMySQL::getInstance()->execQuery($sql);
$resultAFact=CMySQL::getInstance()->execQuery($sqlAFact);

date_default_timezone_set("Europe/Paris");
$today=getdate();
$periodeMax=$today["year"]."-12";

$data=array();
$dataAFact=array();

$row=mysql_fetch_array($result);
$rowAFact=mysql_fetch_array($resultAFact);

$annee=$today["year"]-NB_ANNEE+1;
$mois=1;

$bplots=array();
$bplotsAFact=array();
$accBarPlot=array();

$color=array("blue", "orange", "green");
$idAnnee=0;
do
{
	$periode=$annee."-".str_pad($mois, 2, "0", STR_PAD_LEFT);
	$value=0;
	$valueAFact=0;
	$valueNbFact=0;
	if($row)
	{
		if($periode == $row["periode"])
		{
			$value=$row["ca"]*1;
			$row=mysql_fetch_array($result);
		}	
	}
	
	if($rowAFact)
	{
		if($periode == $rowAFact["periode"])
		{
			$valueAFact=$rowAFact["ca"]*1;
			$rowAFact=mysql_fetch_array($resultAFact);
		}	
	}

	$data[$idAnnee][$mois-1]=$value;
	$dataAFact[$idAnnee][$mois-1]=$valueAFact;
	$dataNbFact[$idAnnee][$mois-1]=$valueNbFact;

	$mois++;
	if($mois == 13)
	{
		$bplots[$idAnnee]=new BarPlot($data[$idAnnee]);
		$bplots[$idAnnee]->SetFillColor($color[$idAnnee]);
		$bplots[$idAnnee]->SetLegend("Année $annee");
		
		$bplotsAFact[$idAnnee]=new BarPlot($dataAFact[$idAnnee]);
		$bplotsAFact[$idAnnee]->SetFillColor("red");
	
		$accBarPlot[$idAnnee] = new AccBarPlot(array($bplots[$idAnnee], $bplotsAFact[$idAnnee]));

		$idAnnee++;
		$annee++;
		$mois=1;
	}
}while($periode < $periodeMax);

$bplotsAFact[NB_ANNEE-1]->SetLegend("Reste à facturer");
$gbarplot=new GroupBarPlot($accBarPlot);

$graph->Add($gbarplot);

/*echo "<pre>";
print_r($data);
print_r($dataAFact);
print_r($dataNbFact);
echo "</pre>";*/

$graph->stroke();
?>
